package com.yumei.trader.gym.view;

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.yumei.trader.gym.util.DensityUtils;


public abstract  class KlineBaseView extends SurfaceView
        implements SurfaceHolder.Callback {


    protected SurfaceHolder holder;

    //Ｋ线宽度
    public  float[] cells = {0.7f,1,3,5,7,9,11,13,15,17,19,21};
    //默认索引
    protected static int index = 2;
    //默认宽度
    protected float cellWidth;
    //K线间距
    public  float dis = 1.5f;
    //右边距离
    public int rightPad = 50;

    protected Rect rect ;

    //K线区
    protected Rect klineRect ;
    //Ｋ线区高度
    protected int klineHeight;

    //成交量区
    protected Rect volRect;
    //成交量区高度
    protected int volHeight ;

    //指标区
    protected Rect indRect;
    //指标区高度
    protected int indHeight ;


    //日期区
    protected Rect dateRect;

    //区空隙高度
    protected int margin = 15;
    //文字区高度
    protected int txtHeight = 15;
    //屏幕最大Ｋ线数
    protected int maxLen ;


    //字体大小
    public  static float FontSize12 = 12.0f;
    public  static float FontSize14 = 14.0f;


    public KlineBaseView(Context context, AttributeSet attrs) {
        super(context, attrs);

        for(int i=0;i<cells.length;i++){
            cells[i] = DensityUtils.dip2px(context,cells[i]);
        }

        dis = DensityUtils.dip2px(context,dis);

        rightPad = DensityUtils.dip2px(context,rightPad);

        txtHeight = DensityUtils.dip2px(context,txtHeight);
        margin = DensityUtils.dip2px(context,margin);

        FontSize12 = DensityUtils.dip2px(context,12);
        FontSize14 = DensityUtils.dip2px(context, 14);

        holder = getHolder();
        holder.addCallback(this);
    }

    public int getZoomIndex(){
        return index;
    }

    public void setZoomIndex(int index){
        this.index = index;

        calucationScreenMaxTick(index);
    }

    protected abstract void checkMinAndMax();

    protected abstract void repaint();


    @Override
    public void surfaceCreated(SurfaceHolder holder) {

        System.out.println("surfaceCreated");
    }


    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

        rect = new Rect(1,1,width-1,height-1);

        klineHeight = 2*rect.height()/3;
        volHeight = (rect.height()-klineHeight-margin)/2;
        indHeight = volHeight;

        //K线区间
        klineRect = new Rect(1,1,width-2,klineHeight);
        //成交量区量
        volRect = new Rect(1,klineRect.bottom,width-2,klineRect.bottom+volHeight);
        //指标区间
        indRect = new Rect(1,volRect.bottom,width-2,volRect.bottom+indHeight);

        //日期区间
        dateRect = new Rect(1,indRect.bottom,width-2,rect.bottom);

        calucationScreenMaxTick(index);

        repaint();
    }



    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {

        System.out.println("surfaceDestroyed");
    }


    //设算屏幕显示最大TICK数
    protected void calucationScreenMaxTick(int index){

        cellWidth = cells[index];
        maxLen = (int)((rect.width()-rightPad)/(cellWidth+dis));

        checkMinAndMax();
    }

}
